package com.bawei.mall.user.service.Impl;

import com.alibaba.fastjson2.JSONObject;
import com.bawei.common.core.exception.ServiceException;
import com.bawei.mall.product.domain.MallUserAddress;
import com.bawei.mall.user.domain.request.UserInformati;
import com.bawei.mall.user.domain.request.vo.UserVo;
import com.bawei.mall.user.mapper.UserMapper;
import com.bawei.mall.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：张余庆
 * @Package：com.bawei.mall.user.service.Impl
 * @Project：mall_cloud
 * @name：UserServiceImpl
 * @Date：2024/6/27 18:16
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 功能：根据账号查询用户信息
     * @param username
     * @return
     */
    @Override
    public UserInformati getUser(String username) {
        UserInformati user = mapper.getUser(username);
        return user;
    }

    /**
     * 功能：查询用户地址
     * @return
     */
    @Override
    public List<MallUserAddress> userAddress() {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        List<MallUserAddress> list = mapper.userAddress(userId);
        return list;
    }
}
